import { constructMockData } from '@/mock/mock'
import { AxiosRequestConfig } from 'axios'
import { dayjs } from 'element-plus'
import Mock from 'mockjs'
import { LivenessType } from '@/enums/constEnums'

/**
 * @description 活跃度模块 mock
 * @param config
 */
export const livenessRes = (config: AxiosRequestConfig<any>) => {
  const { dateRange, type, dateType } = JSON.parse(config.data)
  // 计算需要多少天的数据
  const [startDate, endDate] = dateRange
  const diffDays = dayjs(endDate).diff(startDate, 'day') + 1
  // 生成数据
  let data: any[]
  if (dateType === LivenessType.Retain) {
    data = Array.from({ length: diffDays }).map((item, index) => {
      const mockObj = {
        newUserCount: Mock.Random.integer(100, 1000),
        day1RetainedUserCount: Mock.Random.integer(0, 1000),
        day1RetentionRate: 0,
        day3RetainedUserCount: Mock.Random.integer(0, 1000),
        day3RetentionRate: 0,
        day7RetainedUserCount: Mock.Random.integer(0, 1000),
        day7RetentionRate: 0,
        day14RetainedUserCount: Mock.Random.integer(0, 1000),
        day14RetentionRate: 0,
        day30RetainedUserCount: Mock.Random.integer(0, 1000),
        day30RetentionRate: 0,
      }
      return {
        type,
        date: dayjs(startDate).add(index, 'day').format('YYYY-MM-DD'),
        ...mockObj,
        day1RetentionRate: +(
          (mockObj.day1RetainedUserCount / mockObj.newUserCount) *
          100
        ).toFixed(2),
        day3RetentionRate: +(
          (mockObj.day3RetainedUserCount / mockObj.newUserCount) *
          100
        ).toFixed(2),
        day7RetentionRate: +(
          (mockObj.day7RetainedUserCount / mockObj.newUserCount) *
          100
        ).toFixed(2),
        day14RetentionRate: +(
          (mockObj.day14RetainedUserCount / mockObj.newUserCount) *
          100
        ).toFixed(2),
        day30RetentionRate: +(
          (mockObj.day30RetainedUserCount / mockObj.newUserCount) *
          100
        ).toFixed(2),
      }
    })
  } else {
    data = Array.from({ length: diffDays }).map((item, index) => {
      const mockObj = {
        newUserCount: Mock.Random.integer(100, 1000),
        day1ActiveUserCount: Mock.Random.integer(0, 1000),
        day1ActiveRate: 0,
        day3ActiveUserCount: Mock.Random.integer(0, 1000),
        day3ActiveRate: 0,
        day7ActiveUserCount: Mock.Random.integer(0, 1000),
        day7ActiveRate: 0,
        day14ActiveUserCount: Mock.Random.integer(0, 1000),
        day14ActiveRate: 0,
        day30ActiveUserCount: Mock.Random.integer(0, 1000),
        day30ActiveRate: 0,
      }
      return {
        type,
        date: dayjs(startDate).add(index, 'day').format('YYYY-MM-DD'),
        ...mockObj,
        day1ActiveRate: +(
          (mockObj.day1ActiveUserCount / mockObj.newUserCount) *
          100
        ).toFixed(2),
        day3ActiveRate: +(
          (mockObj.day3ActiveUserCount / mockObj.newUserCount) *
          100
        ).toFixed(2),
        day7ActiveRate: +(
          (mockObj.day7ActiveUserCount / mockObj.newUserCount) *
          100
        ).toFixed(2),
        day14ActiveRate: +(
          (mockObj.day14ActiveUserCount / mockObj.newUserCount) *
          100
        ).toFixed(2),
        day30ActiveRate: +(
          (mockObj.day30ActiveUserCount / mockObj.newUserCount) *
          100
        ).toFixed(2),
      }
    })
  }

  return constructMockData(data)
}
